<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import wsjzglService from '@/api/wsjzgl'
import { useMainStore } from '@/store/main'
import { useRoute } from 'vue-router'
const route = useRoute()
const store = useMainStore()
const emit = defineEmits(['refresh'])

const isLoading = ref(false)
const ruleFormRef = ref<FormInstance>()

//打开窗口

const dialogVisible = ref(false)
const nodeData = ref()

const openDialog = (data: any) => {
  nodeData.value = data
  dialogVisible.value = true
}

const ruleForm = ref({
  hdyz: '',
})

/* 表单校验 */
const rules = ref<FormRules>({
  hdyz: [
    {
      required: true,
      message: '请输入',
      trigger: 'change',
    },
  ],
})

const save = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      const params = {
        ajbh: store.nodeTreeData.ajbh,
        bmsah: route.query.bmsah,
        gdbh: route.query.gdbh,
        mlbh: store.nodeTreeData.bh,
        wjbhList: nodeData.value.map((el: any) => el.wjbh),
      }
      isLoading.value = true
      wsjzglService
        .pictureQhb(params, route.query.ajbh as string)
        .then(() => {
          ElMessage({
            type: 'success',
            message: '操作成功',
          })
          closeDialog()
          emit('refresh')
        })
        .catch((err) => {
          !err.isError && ElMessage({ type: 'warning', message: err.message })
        })
        .finally(() => {
          isLoading.value = false
        })
    }
  })
}

/*
 * 关闭弹框
 */
const closeDialog = () => {
  dialogVisible.value = false
}

defineExpose({ openDialog })
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    title="去黑边"
    width="25%"
    :close-on-click-modal="false"
    :before-close="closeDialog"
  >
    <el-form ref="ruleFormRef" label-width="auto" :model="ruleForm" :rules="rules">
      <el-form-item label="灰度值阈值" prop="hdyz" style="width: 100%">
        <el-input-number
          v-model="ruleForm.hdyz"
          :controls="false"
          placeholder="请输入"
          style="width: 100%"
        >
        </el-input-number>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="save(ruleFormRef)" :loading="isLoading">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style></style>
